home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / TEX-UTIL / DVIPS_55 / dvips / src / c / squeeze < prev    next >
Text File  |  1994-05-06  |  3KB  |  147 lines

  1. /*
  2.  *   This software is Copyright 1988 by Radical Eye Software.
  3.  */
  4. /*
  5.  *   This routine squeezes a PostScript file down to its
  6.  *   minimum.  We parse and then output it.
  7.  */
  8. #include <stdio.h>
  9. #define LINELENGTH (72)
  10. #define BUFLENGTH (1000)
  11. #undef putchar
  12. #define putchar(a) (void)putc(a, out) ;
  13. FILE *in, *out ;
  14. static int linepos = 0 ;
  15. static int lastspecial = 1 ;
  16. extern int strlen() ;
  17. /*
  18.  *   This next routine writes out a `special' character.  In this case,
  19.  *   we simply put it out, since any special character terminates the
  20.  *   preceding token.
  21.  */
  22. void specialout(c)
  23. char c ;
  24. {
  25.    if (linepos + 1 > LINELENGTH) {
  26.       putchar('\n') ;
  27.       linepos = 0 ;
  28.    }
  29.    putchar(c) ;
  30.    linepos++ ;
  31.    lastspecial = 1 ;
  32. }
  33. void strout(s)
  34. char *s ;
  35. {
  36.    if (linepos + strlen(s) > LINELENGTH) {
  37.       putchar('\n') ;
  38.       linepos = 0 ;
  39.    }
  40.    linepos += strlen(s) ;
  41.    while (*s != 0)
  42.       putchar(*s++) ;
  43.    lastspecial = 1 ;
  44. }
  45. void cmdout(s)
  46. char *s ;
  47. {
  48.    int l ;
  49.  
  50.    l = strlen(s) ;
  51.    if (linepos + l + 1 > LINELENGTH) {
  52.       putchar('\n') ;
  53.       linepos = 0 ;
  54.       lastspecial = 1 ;
  55.    }
  56.    if (! lastspecial) {
  57.       putchar(' ') ;
  58.       linepos++ ;
  59.    }
  60.    while (*s != 0) {
  61.       putchar(*s++) ;
  62.    }
  63.    linepos += l ;
  64.    lastspecial = 0 ;
  65. }
  66. char buf[BUFLENGTH] ;
  67. #ifndef VMS
  68. void
  69. #endif
  70. main(argc, argv)
  71. int argc ;
  72. char *argv[] ;
  73. {
  74.    int c ;
  75.    char *b ;
  76.    char seeking ;
  77.    extern void exit() ;
  78.  
  79.    if (argc > 3 || (in=(argc < 2 ? stdin : fopen(argv[1], "r")))==NULL ||
  80.                     (out=(argc < 3 ? stdout : fopen(argv[2], "w")))==NULL) {
  81.       (void)fprintf(stderr, "Usage:  squeeze [infile [outfile]]\n") ;
  82.       exit(1) ;
  83.    }
  84.    (void)fprintf(out, "%%!\n") ;
  85.    while (1) {
  86.       c = getc(in) ;
  87.       if (c==EOF)
  88.          break ;
  89.       if (c=='%') {
  90.          while ((c=getc(in))!='\n') ;
  91.       }
  92.       if (c <= ' ')
  93.          continue ;
  94.       switch (c) {
  95. case '{' :
  96. case '}' :
  97. case '[' :
  98. case ']' :
  99.          specialout(c) ;
  100.          break ;
  101. case '<' :
  102. case '(' :
  103.          if (c=='(')
  104.             seeking = ')' ;
  105.          else
  106.             seeking = '>' ;
  107.          b = buf ;
  108.          *b++ = c ;
  109.          do {
  110.             c = getc(in) ;
  111.             if (b > buf + BUFLENGTH-2) {
  112.                (void)fprintf(stderr, "Overran buffer seeking %c", seeking) ;
  113.                exit(1) ;
  114.             }
  115.             *b++ = c ;
  116.             if (c=='\\')
  117.                *b++ = getc(in) ;
  118.          } while (c != seeking) ;
  119.          *b++ = 0 ;
  120.          strout(buf) ;
  121.          break ;
  122. default:
  123.          b = buf ;
  124.          while ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||
  125.                 (c>='0'&&c<='9')||(c=='/')||(c=='@')||
  126.                 (c=='!')||(c=='"')||(c=='&')||(c=='*')||(c==':')||
  127.                 (c==',')||(c==';')||(c=='?')||(c=='^')||(c=='~')||
  128.                 (c=='-')||(c=='.')||(c=='#')||(c=='|')||(c=='_')||
  129.                 (c=='=')||(c=='$')||(c=='+')) {
  130.             *b++ = c ;
  131.             c = getc(in) ;
  132.          }
  133.          if (b == buf) {
  134.             (void)fprintf(stderr, "Oops!  Missed a case: %c.\n", c) ;
  135.             exit(1) ;
  136.          }
  137.          *b++ = 0 ;
  138.          (void)ungetc(c, in) ;
  139.          cmdout(buf) ;
  140.       }
  141.    }
  142.    if (linepos != 0)
  143.       putchar('\n') ;
  144.    exit(0) ;
  145.    /*NOTREACHED*/
  146. }
  147.